//
//  TableViewController.swift
//  MoyoungCommon
//
//  Created by 李然 on 2022/7/19.
//

import UIKit

open class TableViewController: UITableViewController {
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        makeUI()
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        updateUI()
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.interactivePopGestureRecognizer!.delegate = self
    }
    
    @objc open func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc open func dismissAction() {
        self.dismiss(animated: true)
    }
    
    open func makeUI() {
        
        self.view.backgroundColor = .white
        
        self.tableView.tableFooterView = View()
        self.tableView.backgroundColor = .white
    }
    
    open func updateUI() {

    }
    
    open func bindViewModel() {
        
    }
    
    open func sizeHeaderToFit() {
        let headerView = tableView.tableHeaderView
        headerView?.setNeedsLayout()
        headerView?.layoutIfNeeded()
        
        let height = headerView?.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height ?? 0
        headerView?.height = height
        tableView.tableHeaderView = headerView
    }
    
    open override func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

    open override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
}

extension TableViewController: UIGestureRecognizerDelegate {
    
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:
        UIGestureRecognizer) -> Bool {
        return true
    }
    
    open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
            return (self.navigationController?.viewControllers.count)! > 1
        }
        return true
    }
}
